import {request} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {login} from "../../utils/asyncWx.js";

Page({
    // 获取用户信息
    async handleGetUserInfo(e) {
        try {

            console.log(1);
            // 1 获取用户信息
            const {encryptedData, rawData, iv, signature} = e.detail;
            console.log(2);
            // 2 获取小程序登录成功后的code
            const {code} = await login();
            console.log(3);
            const loginParams = {encryptedData, rawData, iv, signature, code};
            console.log(4);
            //  3 发送请求 获取用户的token
            const {token} = await request({url: "/users/wxlogin", data: loginParams, method: "post"});
            console.log(5);
            // 4 把token存入缓存中 同时跳转回上一个页面
            wx.setStorageSync("token", token);
            wx.navigateBack({
                delta: 1
            });

        } catch (error) {
            console.log(error);
        }
    }
})